﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Top;
using Leefish.Market.PRO.BLL;
using Leefish.Market.PRO.Entity;
using JCW.Common;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;
using LitJson;
using Leefish.Market.PRO;
using System.Collections;
public partial class _Default : PageBase
{
    public string data = "{}";
    protected void Page_Load(object sender, EventArgs e)
    {
        Transfers tf = new Transfers();
        string nick = ReqUtility.GetQueryString("nick");
        // TODO:
        nick = "sandbox_leefish_01";
        if (!string.IsNullOrEmpty(nick))
        {
            EnRoller en = BzRoller.Get(nick);
            if (en != null && DateTime.Now >= en.BeginTime&& DateTime.Now <= en.EndTime) {
                IList<EnAward> list = BzAward.Get(nick);
                string strList = "";
                if (list != null && list.Count > 0) {
                    //en.ListAward = list as List<EnAward>;
                    foreach (EnAward a in list) {
                        strList += string.Format("{0},{1},{2}|", a.AID, a.AName, a.ATypeID);
                    }
                }
                if (strList.EndsWith("|"))
                    strList = strList.Substring(0, strList.Length - 1);

                tf.AddColumn("begin", en.BeginTime.ToString("yyyy年MM月dd日"));
                tf.AddColumn("last", en.EndTime.ToString("yyyy年MM月dd日"));
                tf.AddColumn("notice", en.Notice);
                tf.AddColumn("share", en.Share);
                tf.AddColumn("name", en.AName);
                tf.AddColumn("ni", nick);
                tf.AddColumn("dt", GetRollerDetails(en));
                tf.AddColumn("data", en);
                tf.AddColumn("alist", strList);
               
            }
        }
        tf.AddColumn("list",GetList());
        tf.AddColumn("ua", Request.UserAgent);
        data = tf.ToJson();
    }

    private string GetRollerDetails(EnRoller en)
    {
        string str = "<ul class='fl'>";
        IList<EnRollerDetail> list = BzRollerDetail.GetListByfilter(string.Format(" RollerID={0}", en.ID), 1, 6);
        int i = 1;
        foreach (EnRollerDetail r in list) {
            
            str += string.Format("<li><span>{0}/span>{1}</li>", r.UserNick, r.AName);
            if (i == 3)
            {
                str += "</ul><ul class='fr'>";
            }
            i++;
        }
        str += "</ul>";
        return str;
    }

    private List<EnAward> GetList() {
        List<EnAward> list = new List<EnAward>();
        EnAward a1 = new EnAward();
        a1.AID = 1;
        a1.Probability = 10;
        a1.AName = "奖品1";
        list.Add(a1);

        EnAward a2 = new EnAward();
        a2.AID = 2;
        a2.Probability = 50;
        a2.AName = "奖品2";
        list.Add(a2);

        EnAward a3 = new EnAward();
        a3.AID = 3;
        a3.Probability = 10;
        a3.AName = "奖品3";
        list.Add(a3);

        EnAward a4 = new EnAward();
        a4.AID = 4;
        a4.Probability = 10;
        a4.AName = "奖品4";
        list.Add(a4);

        EnAward a5 = new EnAward();
        a5.AID = 5;
        a5.Probability = 9.5m;
        a5.AName = "奖品5";
        list.Add(a5);

        EnAward a6 = new EnAward();
        a6.AID = 6;
        a6.Probability = 5;
        a6.AName = "奖品6";
        list.Add(a6);

        EnAward a7 = new EnAward();
        a7.AID = 7;
        a7.Probability = 5;
        a7.AName = "奖品7";
        list.Add(a7);

        EnAward a8 = new EnAward();
        a8.AID = 8;
        a8.Probability = 5;
        a8.AName = "奖品8";
        list.Add(a8);

        return list;


    }

    private void TriggerAword() {

        EnRoller roller = new EnRoller();
        roller.BeginTime = DateTime.Now.AddDays(-1);
        roller.EndTime = DateTime.Now.AddDays(10);
        roller.Notice = "这是一个奖品";
        roller.PID = 1;
        roller.PName = "抽奖大风轮";
        roller.Probability = 30;
        roller.ListAward = GetList();

        BzWinBLL bzBLL = new BzWinBLL(roller);
        int rel = 0;
        int max = 1000;
        int cout = 0;
        int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0;
        for (int i = 0; i < max; i++)
        {
            rel = bzBLL.Surprise();
            if (rel > 0)
            {
                cout++;
                switch (rel)
                {
                    case 1: c1++; break;
                    case 2: c2++; break;
                    case 3: c3++; break;
                    case 4: c4++; break;
                    case 5: c5++; break;
                    case 6: c6++; break;
                }
            }

            Response.Write("," + rel);
        }

        Response.Write(string.Format("<BR>：中奖概率{0}%,中奖总个数：{1}，c1:{2},c2:{3},c3:{4},c4:{5},c5:{6},c6:{7}",
            ConvertHelper.ToFloat(100 * cout / max).ToString(), cout,
            c1, c2, c3, c4, c5, c6
            ));

        ///taobao.vas.subscribe.get 订购关系查询  http://api.taobao.com/apidoc/api.htm?spm=0.0.0.0.uDX03r&path=cid:10124-apiId:10586
        /*返回结果
         * {
                "vas_subscribe_get_response": {
                    "article_user_subscribes": {
                        "article_user_subscribe": [{
                            "item_code": "ts-123-1",
                            "deadline": "2000-01-01 00:00:00"
                        }]
                    }
                }
            }

        */

        //ITopClient client = new DefaultTopClient(url, appkey, appsecret);
        //VasSubscribeGetRequest req = new VasSubscribeGetRequest();
        //req.Nick = "hz0799";
        //req.ArticleCode = "ts-1234";
        //VasSubscribeGetResponse response = client.Execute(req);

    }


}